package dao;

import model.Admin;
import util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 管理员数据访问对象类，用于操作管理员表数据
 */
public class AdminDAO {
    /**
     * 添加管理员
     * @param admin 要添加的管理员对象
     */
    public void addAdmin(Admin admin) {
        String sql = "INSERT INTO admin(admin_name, password) VALUES(?,?)";

        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            preparedStatement.setString(1, admin.getAdminName());
            preparedStatement.setString(2, admin.getPassword());

            int rowsAffected = preparedStatement.executeUpdate();
            if (rowsAffected > 0) {
                System.out.println("管理员添加成功！");
            } else {
                System.out.println("管理员添加失败！");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据管理员 ID 获取管理员
     * @param adminId 管理员 ID
     * @return 对应的管理员对象，若未找到则返回 null
     */
    public Admin getAdminById(int adminId) {
        String sql = "SELECT * FROM admin WHERE admin_id = ?";

        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            preparedStatement.setInt(1, adminId);

            ResultSet resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                Admin admin = new Admin();
                admin.setAdminId(resultSet.getInt("admin_id"));
                admin.setAdminName(resultSet.getString("admin_name"));
                admin.setPassword(resultSet.getString("password"));

                return admin;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 获取所有管理员
     * @return 管理员列表
     */
    public List<Admin> getAllAdmins() {
        List<Admin> adminList = new ArrayList<>();
        String sql = "SELECT * FROM admin";

        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             ResultSet resultSet = preparedStatement.executeQuery()) {

            while (resultSet.next()) {
                Admin admin = new Admin();
                admin.setAdminId(resultSet.getInt("admin_id"));
                admin.setAdminName(resultSet.getString("admin_name"));
                admin.setPassword(resultSet.getString("password"));

                adminList.add(admin);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return adminList;
    }

    /**
     * 验证管理员登录
     * @param adminName 管理员名称
     * @param password 密码
     * @return 若管理员名称和密码匹配则返回对应的管理员对象，否则返回 null
     */
    public Admin validateAdminLogin(String adminName, String password) {
        String sql = "SELECT * FROM admin WHERE admin_name = ? AND password = ?";

        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            preparedStatement.setString(1, adminName);
            preparedStatement.setString(2, password);

            ResultSet resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                Admin admin = new Admin();
                admin.setAdminId(resultSet.getInt("admin_id"));
                admin.setAdminName(resultSet.getString("admin_name"));
                admin.setPassword(resultSet.getString("password"));

                return admin;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }
}